package com.tencent.welink.api.msg;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tencent.welink.api.OpenApiTokenChange;
import com.tencent.welink.api.base.WelinkOpenApi;
import com.tencent.welink.config.BussinessConfig;
import com.tencent.welink.util.CrontabUtil;
import com.tencent.welink.util.HttpPoolUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.util.Map;

/**
 * @Description: 获取文件上传
 * 通过对象存储（Cloud Object Storage，简称：COS）发送文件到设备，获取文件在COS上的信息
 * @Author: limye
 * @CreateDate: 2020-6-8
 * @Version: V1.0
 */
public class MessageSendFile extends WelinkOpenApi {

    private static final Logger log = LogManager.getLogger(MessageSendFile.class);

    public MessageSendFile(){
        super("/common/msg/sendFile",true);
    }


    @Override
    public void displayResult(String result) {
    }


    public static void main(String[] args) throws Exception {
        new BussinessConfig();
        CrontabUtil.addTaskInRate(new OpenApiTokenChange(), BussinessConfig.loginDurationMinutes,true);

        //实例化对象
        MessageSendFile instance = new MessageSendFile();
        /**
         * 字段名称	字段类型	是否必须	字段描述
         * token	String	是	鉴权参数：登录获取的动态密钥
         * iotim_ticket	String	是	鉴权参数：登录获取的物联票据
         * wIds	String	是	设备 wId，可以以","分隔，发送给多个设备
         * fileName	String	是	上传的文件名
         * fileSize	Integer	是	上传的文件大小，单位：byte
         * hashCode	String	是	本次上传文件的 hash 值
         * fileMD5	String	是	本次上传文件的 MD5 值
         * data	String	是	给设备发送的自定义消息内容
         */
        //添加参数
        String fileName = "D:\\ip.txt";
        File file = new File(fileName);

        Map<String,Object> map = instance.getExtendsPara();
        map.put("dins","300000000000070420");
        map.put("fileName","ip.txt");
        map.put("fileSize",file.length());
        map.put("hashCode",file.hashCode());
        map.put("fileMD5", DigestUtils.md5Hex(new FileInputStream(fileName)));
        map.put("data","upload ip.txt");

        //执行HTTP请求及展示结果
        String reslut = instance.DoHttp();
        JSONObject jsonObject = JSON.parseObject(reslut);
        jsonObject = jsonObject.getJSONObject("data");
        String fileId = jsonObject.getString("file_id");
        String upload_url = jsonObject.getString("url");
        log.info("fileid:{}",fileId);
        log.info("url:{}",upload_url);
        //{"code":0,"data":{"x-cos-content-sha1":"xxx","file_id":"a8f538d4-566f-45e8-a520-1e34d44218ae","host":"temporary-file-upload-137-1300142618.cos.ap-guangzhou.myqcloud.com","cos_authorization":"q-sign-algorithm=sha1&q-ak=AKIDRjv6D9p8x7A0teupMktwseEXo7LUfN2S&q-sign-time=1594188464;1594192064&q-key-time=1594188464;1594192064&q-header-list=host;x-cos-content-sha1&q-url-param-list=&q-signature=b277965739e2c71080123cf61844460a8b896aa9","url":"http://temporary-file-upload-137-1300142618.cos.ap-guangzhou.myqcloud.com/iot_cos/20200708/a8f538d4-566f-45e8-a520-1e34d44218ae/test"},"message":"OK"}

        //上传文件
        log.info(HttpPoolUtils.httpPut(upload_url,"D:\\ip.txt"));


        CrontabUtil.shutdown();
    }
}
